package com.library.dao;

import java.sql.*;

/**
 * @Author : 雾眠气泡水
 * @Date : 2024/9/15 -- 00:51
 * @Description :
 */
public class BaseDao {
    public static final String USERNAME = "root";
    public static final String PASSWORD = "123456";
    public static final String URL = "jdbc:mysql://localhost:3306/librarysystem";
    public static final String DRIVER_CLASS_NAME = "com.mysql.cj.jdbc.Driver";


    public static Connection conn=null;
    public static PreparedStatement ps = null;

    static {
        try {
            Class.forName(DRIVER_CLASS_NAME);
        } catch (Exception e) {
            System.out.println("驱动加载失败");
        }

    }

    /**
     * 获取数据库连接
     */

    public static Connection getConnection() {
        Connection con  = null;
        try {
            con  = DriverManager.getConnection(URL,USERNAME,PASSWORD);
        } catch (Exception e) {
            System.out.println("数据库连接失败");
        }
        return con;
    }



    /**
     * 执行增删
     */
    public static ResultSet queryResultSet(String sql) {
        conn=BaseDao.getConnection();//打开连接
        ResultSet rs=null;
        try {
            ps= conn.prepareStatement(sql);
            rs = ps.executeQuery();
        } catch (SQLException e) {
            System.out.println("执行sql语句失败");
            e.printStackTrace();
        }
        return rs;
    }

    /**
     * 执行增删
     */

    public static int executeUpdate(String sql,Object[] param) {
        Connection conn=null;
        PreparedStatement ps = null;
        try {
            conn = getConnection();
            ps = conn.prepareStatement(sql);//预编译sql
            if(param != null) {
                for (int i = 0; i < param.length; i++) {
                    ps.setObject(i+1, param[i]);
                }
            }
            return ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            closeAll(conn, ps);
        }
        return -1;
    }



    /**
     * 释放资源Connection，PreparedStatement
     * */


    public static void closeAll(Connection conn,PreparedStatement ps) {
        try {
            if (ps != null) {
                ps.close();
            }
            if (conn != null) {
                conn.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 释放资源Connection，PreparedStatement，ResultSet
     * */

    public static void closeAll(Connection con, Statement st, ResultSet rs) {
        if(rs != null) {
            try {
                rs.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if(st != null) {
            try {
                st.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if(con != null) {
            try {
                con.close();
            } catch (Exception e) {
                System.out.println("连接关闭失败");
            }
        }
    }


}
